// Modby Viyan Pradita
// http://kode-blogger.blogspot.com

function showRecentComments(json) {
    var entry = json.feed.entry,
        total = json.feed.openSearch$totalResults.$t, // Get the comments total result
        skeleton = "",
        oldCount = parseInt(document.getElementById(cm_config.tt_id).innerHTML,10); // Get the older comments total

    // Compare the older comments total with the new comments total.
    // If it's greater, then => show the warning of `the new comments total` minus `the older comments total`
    if (oldCount < total) {
        if (cm_config.alert) {
            alert((total-oldCount)+cm_config.new_cm);
        } else {
            document.title = '('+(total-oldCount)+cm_config.new_cm+') '+document.title;
        }
    }

    // Hanya untuk recent comment widget
    skeleton = '<ul class="cm-outer">';
    for (var i = 0; i < entry.length; i++) {
        for (var j = 0; j < entry[i].link.length; j++) {
            if (entry[i].link[j].rel == 'alternate') {
                link = entry[i].link[j].href;
                break;
            }
        }
        var dash = link.lastIndexOf('/')+1,
            dot = link.lastIndexOf('.'),
            title = link.split('-').join(" ").substring(dash,dot)+'&hellip;';
            author = entry[i].author[0],
            name = author.name.$t,
            avatar = author.gd$image.src.replace(/\/s[0-9]+(\-c|\/)/,"/s"+cm_config.t_w+"$1").replace(/http\:\/\/www.google.com\/url\?source\=imglanding(.*?)q\=/i,"").replace(/\.(jpg|jpeg|png|bmp|gif)(.*?)$/i,".$1"),
            profile = (author.uri) ? author.uri.$t : "#nope",
            date = entry[i].gd$extendedProperty[1].value,
            content = ("content" in entry[i]) ? entry[i].content.$t.replace(/<(.*?)>/g, "") : "",
            nt = (cm_config) ? ' target="_blank"' : '';
    
        skeleton += '<li>';
        skeleton += '<div class="cm-header"><strong><a href="'+link+'" title="'+title+'"'+nt+'>'+name+'</a> berkomentar pada:</strong> '+title+' </div>';
        skeleton += '<div class="cm-content"><a href="'+profile+'" title="'+name+'"'+nt+'><img alt="Loading..." style="width:'+cm_config.t_w+'px;height:'+cm_config.t_h+'px;" src="'+avatar+'"></a>';
        skeleton += '<span class="cm-text">'+content+'</span>';
        skeleton += '</div></li>';
    }
    skeleton += '</ul>';
    document.getElementById(cm_config.ct_id).innerHTML = skeleton;
    document.getElementById(cm_config.tt_id).innerHTML = total;    
}

(function() {
    var head = document.getElementsByTagName('head')[0],
        script = document.createElement('script');
        script.type = "text/javascript";
        script.id = "cm-feed-script";
        script.src = cm_config.home_page+"/feeds/comments/default?alt=json-in-script&redirect=false&max-results="+cm_config.max_result+"&callback=showRecentComments";
    head.appendChild(script);
    setInterval(function() {
        var newScript = document.createElement('script');
            newScript.type = "text/javascript";
            newScript.id = "cm-feed-script";
            newScript.src = cm_config.home_page+"/feeds/comments/default?alt=json-in-script&redirect=false&max-results="+cm_config.max_result+"&callback=showRecentComments";
        var oldScript = document.getElementById('cm-feed-script');
            oldScript.parentNode.removeChild(oldScript);
        head.appendChild(newScript);
    }, cm_config.interval);
})();